package com.itheima.service.db.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.soul.SoulQuestion;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.mapper.SoulQuestionMapper;
import com.itheima.service.db.SoulQuestionService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class SoulQuestionServiceImpl implements SoulQuestionService {

    @Autowired
    SoulQuestionMapper soulQuestionMapper;


    @Override
    public List<SoulQuestion> findByLevel(Long examId) {
        return soulQuestionMapper.findByLevel(examId);
    }

    @Override
    public Long findExamIdByQuestionId(String questionId) {
        return soulQuestionMapper.findExamIdByQuestionId(questionId);
    }

    @Override
    public PageBeanVo findByPage(int pageNumber, int pageSize) {
        Page<SoulQuestion> page = new Page<>(pageNumber, pageSize);
        page = (Page<SoulQuestion>) soulQuestionMapper.selectPage(page,null);

        return new PageBeanVo(pageNumber,pageSize,page.getTotal(),page.getRecords());
    }

    @Override
    public void save(SoulQuestion soulQuestion) {
        soulQuestionMapper.insert(soulQuestion);
    }

    @Override
    public int findQuestionId() {
        List<SoulQuestion> soulQuestionList = soulQuestionMapper.selectList(null);
        SoulQuestion soulQuestion = soulQuestionList.get(soulQuestionList.size()-1);
        return NumberUtil.parseInt(soulQuestion.getId());
    }

    @Override
    public void deleteById(Long questionId) {
        soulQuestionMapper.deleteById(questionId);
    }

    @Override
    public int findExamId(Long questionId) {
        return soulQuestionMapper.findExamId(questionId);

    }

    @Override
    public SoulQuestion findById(Long questionId) {

        return soulQuestionMapper.selectById(questionId);
    }

    @Override
    public void update(SoulQuestion soulQuestion) {
        soulQuestionMapper.updateById(soulQuestion);
    }

}
